package cn.windstudio;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Map extends JFrame {

	public static final Coordinate mapSize = new Coordinate(760, 600);
	private static final long serialVersionUID = 1L;
	private BufferedImage head, node, food, backGround;
	public MyPanel myPanel;
	private Vector<snakeNode> vt = SnakeManager.getManager().getVt();
	private String score;
	

	public Map() {
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void init() throws Exception {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300, 200, 776, 638);
		head = ImageIO.read(new File("pic/head.png"));
		node = ImageIO.read(new File("pic/node.png"));
		food = ImageIO.read(new File("pic/food.png"));
		backGround = ImageIO.read(new File("pic/backGround.jpg"));
		myPanel = new MyPanel();
		myPanel.setPreferredSize(new Dimension(760, 600));
		getContentPane().add(myPanel);
		this.setIconImage(ImageIO.read(new File("pic/icon.png")));

		this.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_UP:
					SnakeManager.getManager().setPOINT(SnakeManager.UP);
					break;
				case KeyEvent.VK_RIGHT:
					SnakeManager.getManager().setPOINT(SnakeManager.RIGHT);
					break;
				case KeyEvent.VK_DOWN:
					SnakeManager.getManager().setPOINT(SnakeManager.DOWN);
					break;
				case KeyEvent.VK_LEFT:
					SnakeManager.getManager().setPOINT(SnakeManager.LEFT);
				default:
					break;
				}
			}
		});
	}

	class MyPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		

		public void paint(Graphics g) {
			
			if (!SnakeManager.getManager().isGAMEOVER()) {
				g.drawImage(backGround, 0, 0, null);
				score = String.valueOf(vt.size()-2);
				g.setColor(Color.lightGray);
				g.setFont(new Font("Consolas", Font.PLAIN, 60));
				g.drawString(score, 20, 60);
				g.drawChars(score.toCharArray(), 0, score.length(), 0, 0);
				g.drawImage(food, SnakeManager.getManager().getFoodXY().X, SnakeManager.getManager().getFoodXY().Y,40,40,
						null);
				g.drawImage(head, vt.get(0).getXY().X, vt.get(0).getXY().Y,40,40, null);
				for (int i = 1; i < vt.size(); i++) {
					if (vt.get(i).getXY().X != -1 || vt.get(i).getXY().Y != -1)
						g.drawImage(node, vt.get(i).getXY().X, vt.get(i).getXY().Y, null);
				}
			}else {
				try {
					try {
						Thread.sleep(750);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					g.drawImage(ImageIO.read(new File("pic/gameover.jpg")), 0, 0, null);
					score = String.valueOf(vt.size()-2);
					g.setColor(Color.lightGray);
					g.setFont(new Font("Consolas", Font.PLAIN, 60));
					g.drawString(score, 20, 60);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
